﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace RecruitmentProcessSystem
{
    public partial class frmEmployeeManager : Form
    {
        String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection conn;
        SqlCommand cmd, cmd_add, cmd_edit, cmd_del;
        SqlDataAdapter da;
        DataSet ds;
        String iid = "";
        public frmEmployeeManager()
        {
            InitializeComponent();
        }

        private void btnCance_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(conStr);
            conn.Open();
            String time = dtpDatetime.Value.ToString();
            String name = txtName.Text;
            String dangnhap = txtDangnhap.Text;
            String matkhau = txtMatkhau.Text;
            String phongban = txtPhongban.Text;
            String role = txtRole.Text;
            cmd_add = new SqlCommand("insert into Nhanvien(tennv,tendn,matkhau,ngaysinh,phongban,role) values('" + name + "','" + dangnhap + "','" + matkhau + "','" + time + "','" + phongban + "','" + role + "')", conn);
            cmd_add.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("Creat new successfully!");
            this.frmEmployeeManager_Load(sender, e);
        }

        private void frmEmployeeManager_Load(object sender, EventArgs e)
        {
            ds = new DataSet();
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd = new SqlCommand("Select * from Nhanvien", conn);
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Nhanvien");
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!iid.Equals(""))
            {
                if (MessageBox.Show("Do you want to delete?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    conn = new SqlConnection(conStr);
                    conn.Open();
                    cmd_del = new SqlCommand("Delete from Nhanvien where mannv = '" + iid + "'", conn);
                    cmd_del.ExecuteNonQuery();
                    conn.Close();
                    this.frmEmployeeManager_Load(sender, e);
                    MessageBox.Show("Delete successfully!");
                }
            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            row = dataGridView1.Rows[e.RowIndex];
            dtpDatetime.Text = row.Cells[4].Value.ToString();
            txtName.Text = row.Cells[1].Value.ToString();
            txtDangnhap.Text = row.Cells[2].Value.ToString();
            txtMatkhau.Text = row.Cells[3].Value.ToString();
            txtPhongban.Text = row.Cells[5].Value.ToString();
            txtRole.Text = row.Cells[6].Value.ToString();
            iid = row.Cells[0].Value.ToString();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (!iid.Equals(""))
            {
                conn = new SqlConnection(conStr);
                conn.Open();
                String time = dtpDatetime.Value.ToString();
                String name = txtName.Text;
                String dangnhap = txtDangnhap.Text;
                String matkhau = txtMatkhau.Text;
                String phongban = txtPhongban.Text;
                String role = txtRole.Text;
                cmd_edit = new SqlCommand("update Nhanvien set ngaysinh = '" + time + "',tennv = '" + name + "',tendn = '" + dangnhap + "',matkhau = '" + matkhau + "',phongban = '" + phongban + "',role = '" + role + "' where iid = " + iid, conn);
                cmd_edit.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Update successfully!");
                this.frmEmployeeManager_Load(sender, e);
            }
        }
    }
}
